package com.tim.shadow.lib.image;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

/*
 * Author: poper Email:xut@bluemobi.cn
 * Created Date:2013-4-22
 * Copyright @ 2013 BU
 * Description: 类描述
 *
 * History:
 */
public class TImageView extends ImageView {
	private static final int LOADING_THREADS = 10;
	private static ExecutorService threadPool = Executors
			.newFixedThreadPool(LOADING_THREADS);

	private TImageHandler mHandler;

	Context mContext;

	public TImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		mHandler = new TImageHandler(this);
	}

	public TImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TImageView(Context context) {
		this(context, null);
	}

	/**
	 * 
	 * 加载网络图片
	 * 
	 * @author poper 2013-4-22 上午11:23:07
	 * @param url
	 * @return void
	 */
	public void loadImageNet(final String url) {

		threadPool.execute(new Runnable() {
			@Override
			public void run() {
				mHandler.sendMessage(mHandler.obtainMessage(0,
						((TImageApplication) mContext.getApplicationContext())
								.getImageCache().getImageBitMap(mContext, url)));
			}
		});
	}

	public static void cancelLoading() {
		threadPool.shutdownNow();
		threadPool = Executors.newFixedThreadPool(LOADING_THREADS);
	}
}
